package io.renren.common.utils;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.exception.MedicalException;
import io.renren.common.result.ResultCodeEnum;
import io.renren.modules.medical.entity.DoctorInfoEntity;
import io.renren.modules.medical.entity.IllSubEntity;
import io.renren.modules.medical.service.DoctorInfoService;
import io.renren.modules.medical.service.IllSubService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import io.renren.modules.sys.service.SysUserRoleService;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.shiro.SecurityUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Luo Sheng
 * @school YiBin university
 * @create 2023-04-03 11:10
 */
public class DoctorInfoUtils {

    public static DoctorInfoEntity getDoctorInfo(DoctorInfoService doctorInfoService) {
        SysUserEntity userEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        if (userEntity == null){
            throw new MedicalException(HttpStatus.SC_UNAUTHORIZED, "用户还未登陆");
        }

        Long userId = userEntity.getUserId();

        DoctorInfoEntity doctorInfo = doctorInfoService.getByUserId(userId);
        if (doctorInfo != null){
            return doctorInfo;
        }else {
            throw new MedicalException(ResultCodeEnum.HEALTH_GUIDANCE_ERROR.getCode(), "获取当前医生信息不存在");
        }
    }

    // 判断当前是不是管理员
    public static List<Long> getRole(SysUserRoleService sysUserRoleService){
        SysUserEntity userEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        LambdaQueryWrapper<SysUserRoleEntity> userRoleWrapper = new LambdaQueryWrapper<>();
        userRoleWrapper.eq(SysUserRoleEntity::getUserId, userEntity.getUserId());
        List<SysUserRoleEntity> list = sysUserRoleService.list(userRoleWrapper);
        if (list == null || list.size() <= 0){
            throw new MedicalException(org.apache.http.HttpStatus.SC_FORBIDDEN, "用户无权限");
        }

        return list.stream().map(SysUserRoleEntity::getRoleId).collect(Collectors.toList());

    }

    public static  boolean isGonitis(Long subId, IllSubService illSubService){
        IllSubEntity illSubEntity = illSubService.getById(subId);
        if (subId != 0 && illSubEntity == null){
            throw new MedicalException(ResultCodeEnum.NOT_FIND_ERROR.getCode(), "空对象异常");
        }
        return illSubEntity.getTitle().contains("膝关节炎");
    }

}
